<?php

namespace app\controllers;
use yii;
use app\models\User;
use app\models\Net;

class GameController extends \app\components\Controller
{
    public function actionNetstart()
    {
        return $this->render('netstart');
    }

    public function actionSolostart()
    {
        $user = User::find()->where(['user_id'=>yii::$app->user->identity->user_id])->one();
        return $this->render('start',['score'=>$user->high_score]);
    }

    public function actionSoloajax()
    {
        if(isset($_POST['bestScore']))
        {
            $user = User::find()->where(['user_id'=>yii::$app->user->identity->user_id])->one();
            $user->high_score = $_POST['bestScore'];
            $user->repeatpassword = $user->password;
            if(!$user->save())
            {
                echo "save error";
            }
        }
        exit;
    }

    public function actionNetajax()
    {
        if(isset($_POST['netstart']))
        {
            $net = Net::find()->where(['user_id'=>yii::$app->user->identity->user_id, 'status'=>Net::STATUS_WAIT])->one();
            $net2 = Net::find()->where(['user_id'=>yii::$app->user->identity->user_id, 'status'=>Net::STATUS_BATTLE])->one();
            if($net === NULL && $net2 === NULL)
            {
                $net = new Net();
                $net->status = Net::STATUS_WAIT;
                $net->user_id = yii::$app->user->identity->user_id;
                $net->save();
            }
        }
        if(isset($_POST['wait']))
        {
            $net = Net::find()->where(['status'=>Net::STATUS_WAIT])->all();
            if(count($net) >= 2)
            {
                $net[0]->opponent_id = $net[1]->user_id;
                $net[1]->opponent_id = $net[0]->user_id;
                $net[0]->status = Net::STATUS_BATTLE;
                $net[1]->status = Net::STATUS_BATTLE;
                $net[0]->save();
                $net[1]->save();
            }
            $net = Net::find()->where(['user_id'=>yii::$app->user->identity->user_id, 'status'=>Net::STATUS_BATTLE])->one();
            if($net !== NULL)
            {
                echo "success";
            }
        }
        if(isset($_POST['sentdata']))
        {
            $net = Net::find()->where(['user_id'=>yii::$app->user->identity->user_id, 'status'=>Net::STATUS_BATTLE])->one();
            if($net !== NULL)
            {
                $net->img = $_POST['img'];
                $net->score = $_POST['score'];
                $net->save();
                echo $_POST['img'];
            }
        }
        if(isset($_POST['getdata']))
        {
            $net = Net::find()->where(['opponent_id'=>yii::$app->user->identity->user_id, 'status'=>Net::STATUS_BATTLE])->one();
            $net2 = Net::find()->where(['user_id'=>yii::$app->user->identity->user_id, 'status'=>Net::STATUS_BATTLE])->one();
            if($net !== NULL)
            {
                $json = ['img'=>$net->img, 'score'=>$net->score, 'iscore'=>$net2->score, 'user'=>$net->user->nick_name, 'death'=>$net->death, 'imdeath'=>$net2->death, 'id'=>$net2->opponent_id];
                echo json_encode($json);
            }
            else
            {
                $net = Net::find()->where(['opponent_id'=>yii::$app->user->identity->user_id, 'status'=>Net::STATUS_DIE])->orderBy(['id' => SORT_DESC])->one();
                $net2 = Net::find()->where(['user_id'=>yii::$app->user->identity->user_id, 'status'=>Net::STATUS_DIE])->orderBy(['id' => SORT_DESC])->one();
                if($net !== NULL && $net2 !== NULL)
                {
                    $json = ['img'=>$net->img, 'score'=>$net->score, 'iscore'=>$net2->score, 'user'=>$net->user->nick_name, 'death'=>$net->death, 'imdeath'=>$net2->death, 'alldeath'=>1];
                    echo json_encode($json);
                }
            }
        }
        if(isset($_POST['scoreEnd']))
        {
            $user = User::find()->where(['user_id'=>yii::$app->user->identity->user_id])->one();
            $pponent = User::find()->where(['user_id'=>$_POST['id']])->one(); 
            if($_POST['scoreEnd'] == 1)
            {
                $user->ladder_score +=5;
                $pponent->ladder_score -=5;
            }
            elseif($_POST['scoreEnd'] == 2)
            {
                $user->ladder_score -=5;
                $pponent->ladder_score +=5;
            }
            elseif($_POST['scoreEnd'] == 3)
            {
                $user->ladder_score +=2;
                $pponent->ladder_score +=2;
            }

            $user->repeatpassword = $user->password;
            $pponent->repeatpassword = $pponent->password;
            $user->save();
            $pponent->save();
        }
        if(isset($_POST['death']))
        {
            $net = Net::find()->where(['user_id'=>yii::$app->user->identity->user_id, 'status'=>Net::STATUS_BATTLE])->one();
            $net->death = 1;
            $net->save();
        }
        if(isset($_POST['end']))
        {
            $net = Net::find()->where(['opponent_id'=>yii::$app->user->identity->user_id, 'status'=>Net::STATUS_BATTLE])->one();
            $net2 = Net::find()->where(['user_id'=>yii::$app->user->identity->user_id, 'status'=>Net::STATUS_BATTLE])->one();
            $net->status = Net::STATUS_DIE;
            $net2->status = Net::STATUS_DIE;
            $net->save();
            $net2->save();
        }
 
        exit;
    }


}
